package smart.src;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.openswing.swing.message.receive.java.ErrorResponse;
import org.openswing.swing.message.receive.java.Response;
import org.openswing.swing.message.receive.java.VOListResponse;
import org.openswing.swing.table.client.GridController;
import org.openswing.swing.table.java.GridDataLocator;

import src.DAO.CardDaoImpl;
import src.DAO.CardStateDaoImpl;
import src.VO.CardStateVO;
import src.VO.CardVO;

import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource;

public class CardStateGridFrameController extends GridController implements
		GridDataLocator {

	private CardStateDaoImpl cardStateDAO;
	private ConnectionSource connectionSource;
	private CardStateGridFrame cardStateGridFrame;

	public CardStateGridFrameController(ConnectionSource connectionSource) {
		this.connectionSource = connectionSource;
		cardStateGridFrame = new CardStateGridFrame(this);
	}

	@Override
	public Response loadData(int action, int startIndex, Map filteredColumns,
			ArrayList currentSortedColumns,
			ArrayList currentSortedVersusColumns, Class valueObjectType,
			Map otherGridParams) {
		try {
			cardStateDAO = new CardStateDaoImpl(connectionSource);
			List<CardStateVO> result;
			QueryBuilder<CardStateVO, Integer> cardQb = cardStateDAO.queryBuilder();

			cardQb.where().gt("idstate", 0);
			PreparedQuery<CardStateVO> querry = cardQb.prepare();
			result = cardStateDAO.query(querry);
			return new VOListResponse(result, false, result.size());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ErrorResponse(e.getMessage());
		}
	}

}
